package cn.hill4j.tool.spring.ext.mvc;

import cn.hill4j.tool.spring.ext.mvc.context.DecRequestContext;
import cn.hill4j.tool.spring.ext.mvc.context.RequestContextUtils;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.core.MethodParameter;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.annotation.RequestParamMethodArgumentResolver;
/**
  * ClassName: DecRequestParamMethodArgumentResolver
  * Description: 扩展了请求参数加解密的RequestParamMethodArgumentResolver
  * @author hillchen
  * Date: 2023/8/13
  * @version: 1.0
  */
public class DecRequestParamMethodArgumentResolver extends RequestParamMethodArgumentResolver {
    public DecRequestParamMethodArgumentResolver(boolean useDefaultResolution) {
        super(useDefaultResolution);
    }
    /**
     * @author hillchen
     * Description: 构造方法
     * Date: 2023/8/13 00:36
     * @param beanFactory bean工厂
     * @param useDefaultResolution 是否用默认Resolution
     **/
    public DecRequestParamMethodArgumentResolver(ConfigurableBeanFactory beanFactory, boolean useDefaultResolution) {
        super(beanFactory, useDefaultResolution);
    }

    @Override
    protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest request) throws Exception {
        DecRequestContext context = RequestContextUtils.getReqContext();
        if (context.isAllQueryDes() || (context.isQueryReset() && context.hasResetQuery(name))){
            return context.getResetQuery(name);
        }
        return super.resolveName(name, parameter, request);
    }
}
